﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.ViewCamera.Rgbd.CApRgbdConfigApp
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Forms;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.ViewCamera.Rgbd;

internal class CApRgbdConfigApp
{
  private const string FILE_NAME_RGB = "rgbd_rgb_pic";
  private const string FILE_NAME_DEPTH = "rgbd_depth";
  private const string FILE_NAME_DEPTH_PIC = "rgbd_depth_pic";
  private const string FILE_NAME_HISTO = "rgbd_histo";
  private const string FILE_EXT_PNG = ".png";
  private const string FILE_EXT_DAT = ".dat";
  private const string FILE_FORMAT = "{0}_{1}{2}";
  private TxRgbdViewCamera _cameraObj;
  private ITxRgbdDepthReaderWriter _readerWriter;

  public CApRgbdConfigApp(TxRgbdViewCamera rgbdCamera, ITxRgbdDepthReaderWriter readerWriter)
  {
    this._cameraObj = rgbdCamera;
    this._readerWriter = readerWriter;
    this._cameraObj.UpdateCameraGraphicViewerTitle(this._cameraObj.Configuration.ImageCreation);
    this._cameraObj.AdjustGraphicViewerAspectRatio(this._cameraObj.Configuration);
  }

  public void TakeSnapshot(
    eImageType imagetype,
    string outpuLocation,
    TxRgbdCameraConfiguration camConfig)
  {
    Cursor current = Cursor.Current;
    Cursor.Current = Cursors.WaitCursor;
    try
    {
      this.InternalTakeSnapshot(imagetype, outpuLocation, camConfig);
    }
    catch (DirectoryNotFoundException ex)
    {
      int num = (int) TxMessageBox.Show(RgbdResources.MESSAGE_INVALID_PATH, RgbdResources.MESSAGE_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
    }
    catch (OutOfMemoryException ex)
    {
      int num = (int) TxMessageBox.Show(RgbdResources.MESSAGE_OUT_OF_MEMORY, RgbdResources.MESSAGE_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
    }
    catch (TxRgbdViewCamera.RgbdInitializationException ex)
    {
      int num = (int) TxMessageBox.Show(RgbdResources.MESSAGE_GRAPHIC_VIEWER_CLOSED, RgbdResources.MESSAGE_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
    }
    finally
    {
      Cursor.Current = current;
    }
  }

  public void ApplyChanges(TxRgbdCameraConfiguration newConfiguration)
  {
    if (!TxRgbdUtils.IsRGBDCamera(this._cameraObj.Camera) || !this._cameraObj.Configuration.Equals((object) newConfiguration))
      this.SaveCameraConfiguration(newConfiguration);
    this._cameraObj.InitGraphicViewerCamera();
    this._cameraObj.UpdateCameraGraphicViewerTitle(newConfiguration.ImageCreation);
    if (this._cameraObj.AdjustGraphicViewerAspectRatio(newConfiguration))
      return;
    int num = (int) TxMessageBox.Show(RgbdResources.MESSAGE_ASPECT_RATIO_ERROR, RgbdResources.MESSAGE_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  }

  private void InternalTakeSnapshot(
    eImageType imagetype,
    string outpuLocation,
    TxRgbdCameraConfiguration camConfig)
  {
    if (string.IsNullOrWhiteSpace(outpuLocation) || !Directory.Exists(outpuLocation))
      throw new DirectoryNotFoundException();
    string timestamp = this.TimeStampAsString();
    this._cameraObj.InitGraphicViewerCamera();
    using (TxRgbdOutputData outputData = this._cameraObj.Snapshot(camConfig))
    {
      if (!this._cameraObj.GraphicViewerHasCorrectAspectRatio)
      {
        int num = (int) TxMessageBox.Show(RgbdResources.MESSAGE_ASPECT_RATIO_ERROR, RgbdResources.MESSAGE_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
      }
      if (imagetype == null || imagetype == 2)
      {
        string path2 = $"{"rgbd_rgb_pic"}_{timestamp}{".png"}";
        outputData.RgbBitmap.Save(Path.Combine(outpuLocation, path2), ImageFormat.Png);
      }
      if (imagetype != 2 && imagetype != 1)
        return;
      this.SaveDepthData(timestamp, outputData, camConfig, outpuLocation);
      this.SaveDepthImage(outputData, timestamp, outpuLocation);
    }
  }

  private void SaveDepthData(
    string timestamp,
    TxRgbdOutputData outputData,
    TxRgbdCameraConfiguration cameraConfiguration,
    string outputDir)
  {
    string path2 = $"{"rgbd_depth"}_{timestamp}{".dat"}";
    this._readerWriter.FilePath = Path.Combine(outputDir, path2);
    this._readerWriter.Write(outputData.DepthBuffer, new TxRgbdDepthMetaData()
    {
      Width = cameraConfiguration.DepthWidth,
      Height = cameraConfiguration.DepthHeight,
      FovAngle = cameraConfiguration.DepthFovAngle,
      MinDistance = (float) cameraConfiguration.DepthMinimal,
      MaximalRange = (float) cameraConfiguration.DepthMaximumRange
    });
  }

  private void SaveDepthImage(TxRgbdOutputData outputData, string timestamp, string outputDir)
  {
    if (outputData.DepthBitmap != null)
    {
      string path2 = $"{"rgbd_depth_pic"}_{timestamp}{".png"}";
      outputData.DepthBitmap.Save(Path.Combine(outputDir, path2), ImageFormat.Png);
    }
    if (outputData.HistogramBitmap == null)
      return;
    string path2_1 = $"{"rgbd_histo"}_{timestamp}{".png"}";
    outputData.HistogramBitmap.Save(Path.Combine(outputDir, path2_1), ImageFormat.Png);
  }

  private void SaveCameraConfiguration(TxRgbdCameraConfiguration newConfiguration)
  {
    this._cameraObj.Configuration = newConfiguration;
    this._cameraObj.SaveCameraConfiguration();
    this._cameraObj.Camera.Color = TxColor.TxColorOrange;
    CApVCDisplayCameraEnvelopeApp.the().OnRGBDCameraConfigurationChanged(this._cameraObj.Camera, false);
    TxApplication.RefreshDisplay();
  }

  private string TimeStampAsString() => DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss-fff");
}
